package com.software.graduationproject.modules.shopping.service;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.Page;

import com.software.graduationproject.commons.exception.ServiceException;
import com.software.graduationproject.commons.select.Select;
import com.software.graduationproject.modules.food.entity.Food;
import com.software.graduationproject.modules.shopping.entity.ShoppingCart;
import com.software.graduationproject.modules.shopping.mapper.ShoppingCartMapper;
import com.software.graduationproject.modules.shopping.object.ShoppingCartPocketVo;
import com.software.graduationproject.modules.shopping.object.ShoppingCartVo;
import com.software.graduationproject.modules.shopping.object.ShoppingCartCreateQo;
import com.software.graduationproject.modules.shopping.object.ShoppingCartUpdateQo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * ShoppingCartService : 购物车表
 * ---------------------------------------------------------------------------------------------------------------------
 */
@Service
public class ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    public void createQo(ShoppingCartCreateQo qo){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ShoppingCart::getUserId,qo.getUserId());
        queryWrapper.eq(ShoppingCart::getMarketersId,qo.getMarketersId());
        queryWrapper.eq(ShoppingCart::getFoodId,qo.getFoodId());
        queryWrapper.eq(ShoppingCart::getTaste,qo.getTaste());
        queryWrapper.eq(ShoppingCart::getStatus,0);
        ShoppingCart shoppingCartOld = shoppingCartMapper.selectOne(queryWrapper);
        if (shoppingCartOld != null) {
            shoppingCartOld.setPrice(shoppingCartOld.getPrice() + qo.getPrice());
            shoppingCartOld.setOriginalPrice(shoppingCartOld.getOriginalPrice() + qo.getOriginalPrice());
            shoppingCartOld.setQuantity(shoppingCartOld.getQuantity() + qo.getQuantity());
            shoppingCartMapper.updateById(shoppingCartOld);
        }else {
            ShoppingCart shoppingCart = new ShoppingCart();
            BeanUtil.copyProperties(qo, shoppingCart);
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    public void updateQo(ShoppingCartUpdateQo qo){
        ShoppingCart shoppingCart = shoppingCartMapper.selectById(qo.getId());
        if (shoppingCart == null){
            throw  new ServiceException("数据不存在");
        }
        BeanUtil.copyProperties(qo, shoppingCart);
        shoppingCartMapper.updateById(shoppingCart);
    }
//
//    public ShoppingCartVo selectVo(Long id) {
//        ShoppingCartVo shoppingCartVo = new ShoppingCartVo();
//        ShoppingCart shoppingCart = shoppingCartMapper.select(id);
//        Beans.copyPropertiesNotNull(shoppingCartVo, shoppingCart);
//        return shoppingCartVo;
//    }

    public Page<ShoppingCartVo> selectPageVo(Select select){
        return shoppingCartMapper.selectPageVo(select);
    }

    public List<ShoppingCartVo> selectListVo(Select select) {
        List<ShoppingCartVo> shoppingCartVos = shoppingCartMapper.selectListVo(select);
        Map<Long, List<ShoppingCartVo>> groupedByMarketersId = shoppingCartVos.stream()
                .collect(Collectors.groupingBy(ShoppingCartVo::getMarketersId));
        List<ShoppingCartVo> result = new ArrayList<>();
        groupedByMarketersId.forEach((marketersId, carts) -> {
            ShoppingCartVo originalCart = carts.get(0);
            List<ShoppingCartVo.Foods> foodList = new ArrayList<>();
            for (ShoppingCartVo cart : carts) {
                ShoppingCartVo.Foods food = new ShoppingCartVo.Foods();
                food.setId(cart.getId());
                food.setFoodId(cart.getFoodId());
                food.setFoodName(cart.getFoodName());
                food.setFoodImg(cart.getFoodImg());
                food.setQuantity(cart.getQuantity());
                food.setPrice(cart.getPrice());
                food.setOriginalPrice(cart.getOriginalPrice());
                food.setTaste(cart.getTaste());
                food.setUnitPrice(cart.getUnitPrice());
                food.setDiscount(cart.getDiscount());
                foodList.add(food);
            }
            ShoppingCartVo vo = new ShoppingCartVo();
            BeanUtil.copyProperties(originalCart,vo);
            vo.setFoodList(foodList);
            result.add(vo);
        });
        return result;
    }

    public Integer selectCount(Select select) {
       return shoppingCartMapper.selectCountByUserId(select);
    }

    public ShoppingCartPocketVo selectPriceAndNum(Select select) {
        return shoppingCartMapper.selectPriceAndNum(select);
    }

    public void deleteList(Long []ids) {
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(ShoppingCart::getId,ids);
        shoppingCartMapper.delete(queryWrapper);
    }


}
